package com.wxd.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxd.modules.sys.dao.SysRoleDeptDao;
import com.wxd.modules.sys.dao.SysRoleMenuDao;
import com.wxd.modules.sys.entity.SysRoleDeptEntity;
import com.wxd.modules.sys.entity.SysRoleMenuEntity;
import com.wxd.modules.sys.service.SysRoleDeptService;
import com.wxd.modules.sys.service.SysRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色部门对应关系
 */
@Service("sysRoleDeptService")
public class SysRoleDeptServiceImpl extends ServiceImpl<SysRoleDeptDao, SysRoleDeptEntity> implements SysRoleDeptService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long roleId, List<Long> deptIdList) {
        //先删除角色和部门的关系
        deleteBatch(new Long[]{roleId});
         if(deptIdList.size()==0)
         {
             return;
         }
       //保存角色与菜单关系
        for(Long deptId : deptIdList){
            SysRoleDeptEntity sysRoleDeptEntity = new SysRoleDeptEntity();
            sysRoleDeptEntity.setDeptId(deptId);
            sysRoleDeptEntity.setRoleId(roleId);
            this.save(sysRoleDeptEntity);
        }
    }

    @Override
    public List<Long> queryDeptIdList(Long[] roleIds) {
        return baseMapper.queryDeptIdList(roleIds);
    }

    @Override
    public List<Long> queryDeptIdListByRoleId(Long roleId) {
        return baseMapper.queryDeptIdListByRoleId(roleId);
    }

    @Override
    public int deleteBatch(Long[] roleIds) {
        return baseMapper.deleteBatch(roleIds);
    }
}
